package Maquina_Virtual;

import java.util.ArrayList;

public class Memoria extends ArrayList<String>{
	
	private static final long serialVersionUID = 1L;

	public Memoria(int num){
		super();
		new ArrayList<String>(num);
	}

	public String getValor(int index) throws Excepcion_Maquina_Virtual{
		try{
			String aux = this.get(index);
			if(aux.equals(""))
				throw new Excepcion_Maquina_Virtual("Variable sin inicializar");
			else return aux;
		}
		catch(IndexOutOfBoundsException e){
			throw new Excepcion_Maquina_Virtual("Variable sin inicializar");
		}
	}
	
	@Override
	public String set(int index, String e){
		
		if(this.size()==0){
			for(int i=0; i < index; i++){
				this.add(i,"");
			}
			this.add(index,e);	
		}
		else if(this.size() <= index){
			for(int i=this.size(); i<=index; i++){
				this.add(i, "");
			}
			this.set(index,e);	
		}
		else return super.set(index,e);	
		return e;
	}
	
	
	
}
